package com.stormrage.edu.ctrl.student;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.edu.serv.student.StuNoticeServ;
import com.stormrage.entity.student.StuAcct;
import com.stormrage.entity.student.StuNotice;
import com.stormrage.entity.student.rep.LeaBunchNoticeRep;
import com.stormrage.entity.student.rep.StuNoticeRep;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: StuNoticeCtrl.java
 * @Package com.stormrage.edu.ctrl.student
 * @Description: 通知 Controller
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Controller
@RequestMapping("/notice")
public class StuNoticeCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(StuNoticeCtrl.class);

	@Autowired
	private StuAcctServ stuAcctServ;

	@Autowired
	private StuNoticeServ stunoticeServ;

	@RequestMapping("/")
	public String index(Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 index 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取全体学员通知 ------------------
			List<StuNoticeRep> array = stunoticeServ.queryRepListByBunchId(0);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 index 】-【 GET 】-【 COMPLETED 】");
		return "notice/index";
	}

	@RequestMapping("/{id}")
	public String notice(@PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 notice 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取全体学员通知 ------------------
			StuNotice entity = stunoticeServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("data", entity);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 notice 】-【 GET 】-【 COMPLETED 】");
		return "notice/notice";

	}

	@RequestMapping("/bunch")
	public String bunch(HttpServletRequest request, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 bunch 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ 获取全体学员通知 ------------------
			List<LeaBunchNoticeRep> array = stunoticeServ.queryBunchRepList(curr.getId());

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 bunch 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/bunch";
	}

	@RequestMapping("/bunch/{id}")
	public String list(@PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ 获取全体学员通知 ------------------
			List<StuNoticeRep> array = stunoticeServ.queryRepListByBunchId(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("data", array);
			model.addAttribute("id", id);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/list";
	}

	@RequestMapping("/bunch/{aid}/{id}")
	public String bunchNotice(@PathVariable("aid") Integer aid, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 bunchNotice 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ 获取全体学员通知 ------------------
			StuNotice entity = stunoticeServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("data", entity);
			model.addAttribute("id", aid);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 bunchNotice 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/notice";

	}

}
